import type { RouteRecordRaw } from "vue-router"
import { routerConfig } from "@/router/config"
import { registerNavigationGuard } from "@/router/guard"
import { createRouter } from "vue-router"
import { flatMultiLevelRoutes } from "./helper"

const Layouts = () => import("@/layouts/index.vue")

/**
 * @name 常驻路由
 * @description 除了 redirect/403/404/login
 * 等隐藏页面，其他页面建议设置唯一的 Name 属性
 */
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/login",
    component: () => import("@/pages/userLogin/pwsLogin.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/userReset",
    component: () => import("@/pages/login/userReset/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/redirect",
    component: Layouts,
    meta: {
      hidden: true
    },
    children: [
      {
        path: ":path(.*)",
        component: () => import("@/pages/redirect/index.vue")
      }
    ]
  },
  {
    path: "/403",
    component: () => import("@/pages/error/403.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/404",
    component: () => import("@/pages/error/404.vue"),
    meta: {
      hidden: true
    },
    alias: "/:pathMatch(.*)*"
  },
]

/**
 * @name 动态路由
 * @description 用来放置有权限 (Roles 属性) 的路由 roles: ["admin", "editor"],
 * @description 必须带有唯一的 Name 属性
 */
export const dynamicRoutes: RouteRecordRaw[] = [
  {
    path: "/index",
    component: Layouts,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/pages/index/admin/index.vue"),
        name: "Dashboard",
        meta: {
          title: "首页",
          svgIcon: "dashboard",
          affix: true,
          roles: ["admin"]
        }
      },
      {
        path: "dashboard",
        component: () => import("@/pages/index/teacher/index.vue"),
        name: "Dashboard",
        meta: {
          title: "首页",
          svgIcon: "dashboard",
          affix: true,
          roles: ["teacher"]
        }
      },
      {
        path: "dashboard",
        component: () => import("@/pages/index/student/index.vue"),
        name: "Dashboard",
        meta: {
          title: "首页",
          svgIcon: "dashboard",
          affix: true,
          roles: ["student"]
        }
      },

    ]
  },
  {
    path: "/account",
    component: Layouts,
    redirect: "/account/administrators",
    name: "Account",
    meta: {
      roles: ["admin"],
      title: "账户管理",
      elIcon: "CreditCard",
    },
    children: [
      {
        path: "administrators",
        component: () => import("@/pages/account/administrators.vue"),
        name: "Administrators",
        meta: {
          title: "管理员管理",
          elIcon: "User",
        }
      },{
        path: "teacherAccount",
        component: () => import("@/pages/account/teacher.vue"),
        name: "TeacherAccount",
        meta: {
          roles: ["admin"],
          title: "教师管理",
          elIcon: "Avatar",
        }
      },{
        path: "studentAccount",
        component: () => import("@/pages/account/student.vue"),
        name: "StudentAccount",
        meta: {
          roles: ["admin"],
          title: "学生管理",
          elIcon: "UserFilled",
        }
      },{
        path: "accountData",
        component: () => import("@/pages/account/accountData.vue"),
        name: "AccountData",
        meta: {
          roles: ["admin"],
          title: "用户认证数据",
          elIcon: "FolderChecked",
        }
      }
    ]
  },
  {
    path: "/projectAccount",
    component: Layouts,
    redirect: "/projectAccount/management",
    name: "ProjectAccount",
    meta: {
      title: "项目管理",
      elIcon: "FolderOpened",
    },
    children: [
      {
        path: "management",
        component: () => import("@/pages/project/projectManagement/index.vue"),
        name: "Management",
        meta: {
          title: "项目管理",
          elIcon: "Postcard",
        }
      },{
        path: "examine",
        component: () => import("@/pages/project/examine/index.vue"),
        name: "Examine",
        meta: {
          title: "项目审核",
          roles: ["admin", "teacher"],
          elIcon: "SetUp",
        }
      },{
        path: "type",
        component: () => import("@/pages/project/projectType/projectType.vue"),
        name: "Type",
        meta: {
          title: "项目类型管理",
          roles: ["admin"],
          elIcon: "CopyDocument",
        }
      }
    ]
  },
  {
    path: "/index",
    component: Layouts,
    redirect: "/index/outcomes",
    children: [
      {
        path: "outcomes",
        component: () => import("@/pages/outcomes/admin.vue"),
        name: "Outcomes",
        meta: {
          title: "个人成果",
          roles: ["admin"],
          elIcon: "Discount",
        }
      },
        {
          path: "outcomes",
          component: () => import("@/pages/outcomes/teacher.vue"),
          name: "Outcomes",
          meta: {
            title: "个人成果",
            roles: ["teacher"],
            elIcon: "Discount",
          }
        },
        {
          path: "outcomes",
          component: () => import("@/pages/outcomes/student.vue"),
          name: "Outcomes",
          meta: {
            title: "个人成果",
            roles: ["student"],
            elIcon: "Discount",
          }
        }
    ]
  },
  {
    path: "/index",
    component: Layouts,
    redirect: "/index/notification",
    meta: {
      roles: ["admin"],
    },
    children: [
      {
        path: "notification",
        component: () => import("@/pages/management/notification.vue"),
        name: "Notification",
        meta: {
          roles: ["admin"],
          title: "公告管理",
          elIcon: "Notification",
        }
      }
    ]
  },
  {
    path: "/index",
    component: Layouts,
    redirect: "/index/authentication",
    meta: {
      roles: ["admin"],
    },
    children: [
      {
        path: "authentication",
        component: () => import("@/pages/account/authentication.vue"),
        name: "Authentication",
        meta: {
          roles: ["admin"],
          title: "用户认证",
          elIcon: "Connection",
        }
      }
    ]
  },
  {
    path: "/index",
    component: Layouts,
    redirect: "/index/grams",
    meta: {
      roles: ["admin", "teacher"],
    },
    children: [
      {
        path: "grams",
        component: () => import("@/pages/funding/index.vue"),
        name: "Grams",
        meta: {
          roles: ["admin", "teacher"],
          title: "项目资金申请",
          elIcon: "Eleme",
        }
      }
    ]
  },
  {
    path: "/comments",
    component: Layouts,
    redirect: "/comments",
    meta: {
      roles: ["admin"],
    },
    children: [
      {
        path: "",
        component: () => import("@/pages/comments/index.vue"),
        name: "Comments",
        meta: {
          title: "评论管理",
          elIcon: "Comment",
        }
      }
    ]
  },
  // {
  //   path: "/comment",
  //   component: Layouts,
  //   redirect: "/comment",
  //
  //   children: [
  //     {
  //       path: "",
  //       component: () => import("@/pages/account/components/richText.vue"),
  //       name: "word",
  //       meta: {
  //         title: "富文本",
  //         elIcon: "Comment",
  //       }
  //     }
  //   ]
  // },
  {
    path: "/userData",
    component: Layouts,
    meta: {
      hidden: true
    },
    children: [
      {
        path: "",
        name: "userData",
        meta: {
          roles: ["admin", "teacher", "student"],
          title: "账户管理",
        },
        component: () => import("@/pages/userData/index.vue")
      }
    ]
  },
  {
    path: "/",
    redirect: "/welcome",
    meta: {
      hidden: true
    },
    children: [
      {
        path: "/welcome",
        name: "欢迎页",
        component: () => import("@/pages/welcome/Welcome.vue"),
        meta: {
          title: "欢迎页",
          hidden: true
        }
      },{
        path: "/forum",
        name: "论坛",
        component: () => import("@/pages/welcome/Forum.vue"),
        meta: {
          title: "论坛",
          hidden: true
        }
      },{
        path: "/Information",
        name: "公告信息",
        meta: {
          title: "公告信息",
        },
        component: () => import("@/pages/welcome/Information.vue")
      },{
        path: '/detail',
        name: '帖子详情',
        meta: {
          title: "帖子详情",
        },
        component: ()=>import('@/pages/welcome/Detail.vue')
      },
      {
        path: '/annDetail',
        name: '公告详情',
        meta: {
          title: "公告详情",
        },
        component: ()=>import('@/pages/welcome/AnnDetails.vue')
      },
      {
        path: '/cementDetails',
        name: '公告',
        meta: {
          title: "公告",
        },
        component: ()=>import('@/pages/welcome/CementDetails.vue')
      },{
        path: '/projectDetails',
        name: '项目',
        meta: {
          title: "项目",
        },
        component: ()=>import('@/pages/welcome/ProjectDetails.vue')
      }
    ]
  },
  {
    path: "/visualization",
    redirect: "/visualization",
    meta: {
      roles: ["admin"],
      hidden: true
    },
    children: [
      {
        path: "/visualization",
        name: "可视化",
        meta: {
          roles: ["admin"],
          title: "可视化",
        },
        component: () => import("@/pages/visualization/index.vue")
      },{
        path: "/userKsh",
        name: "用户管理可视化",
        meta: {
          roles: ["admin"],
          title: "用户管理可视化",
        },
        component: () => import("@/pages/visualization/userManagement.vue")
      }
      ,{
        path: "/projectKsh",
        name: "项目管理可视化",
        meta: {
          roles: ["admin"],
          title: "项目管理可视化",
        },
        component: () => import("@/pages/visualization/projectManagement.vue")
      }
    ]
  }
]

/** 路由实例 */
export const router = createRouter({
  history: routerConfig.history,
  routes: routerConfig.thirdLevelRouteCache ? flatMultiLevelRoutes(constantRoutes) : constantRoutes
})

/** 重置路由 */
export function resetRouter() {
  try {
    // 注意：所有动态路由路由必须带有 Name 属性，否则可能会不能完全重置干净
    router.getRoutes().forEach((route) => {
      const { name, meta } = route
      if (name && meta.roles?.length) {
        router.hasRoute(name) && router.removeRoute(name)
      }
    })
  } catch {
    // 强制刷新浏览器也行，只是交互体验不是很好
    location.reload()
  }
}


// 注册路由导航守卫
registerNavigationGuard(router)
